﻿using Common.Communal;
using ERP.Domain.Scheduling;
using Scheduling.Repository.Interface;

namespace Scheduling.Services.Schedules
{
    public class ScheduleServices : IScheduleServices
    {
        public ScheduleServices(IScheduleRepository scheduleRepository)
        {
            ScheduleRepository = scheduleRepository;
        }

        public IScheduleRepository ScheduleRepository { get; }

        public async Task<ApiResult<int>> AddSchedule(Schedule schedule)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                if (schedule == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "排班信息不能为空";
                    return result;
                }

                if (schedule.TeamId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "TeamId 无效";
                    return result;
                }

                if (schedule.ShiftId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "ShiftId 无效";
                    return result;
                }

                if (schedule.CalenDarId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "CalendarId 无效";
                    return result;
                }

                if (schedule.ScheduleDate == default)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "排班日期不能为空";
                    return result;
                }

                var affected = await ScheduleRepository.AddAsync(schedule);
                result.Code = EnumCode.Ok;
                result.Message = "排班添加成功";
                result.Data = affected;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"添加排班失败：{ex.Message}";
            }

            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> UpdSchedule(Schedule schedule)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                if (schedule == null || schedule.ScheduleId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "排班ID无效";
                    return result;
                }

                var existing = await ScheduleRepository.GetModelAsync(schedule.ScheduleId);
                if (existing == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "要修改的排班不存在";
                    return result;
                }

                existing.TeamId = schedule.TeamId;
                existing.ShiftId = schedule.ShiftId;
                existing.ScheduleDate = schedule.ScheduleDate;
                existing.IsUrgentSchedule = schedule.IsUrgentSchedule;
                existing.IsUrgentOrder = schedule.IsUrgentOrder;
                existing.CalenDarId = schedule.CalenDarId;

                var affected = await ScheduleRepository.Update(existing);
                result.Code = EnumCode.Ok;
                result.Message = "排班修改成功";
                result.Data = affected;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"修改排班失败：{ex.Message}";
            }

            return await Task.FromResult(result);
        }

        public async Task<ApiResult<int>> DeleteSchedule(int scheduleId)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                if (scheduleId <= 0)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "排班ID无效";
                    return result;
                }

                var existing = await ScheduleRepository.GetModelAsync(scheduleId);
                if (existing == null)
                {
                    result.Code = EnumCode.Fail;
                    result.Message = "要删除的排班不存在";
                    return result;
                }

                existing.IsDelete = true;
                var affected = await ScheduleRepository.Update(existing);
                result.Code = EnumCode.Ok;
                result.Message = "排班删除成功";
                result.Data = affected;
            }
            catch (Exception ex)
            {
                result.Code = EnumCode.Fail;
                result.Message = $"删除排班失败：{ex.Message}";
            }

            return await Task.FromResult(result);
        }
    }
}